home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / tutorials / custEducation / opengl1 / lib / init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  10.4 KB  |  396 lines

  1. /*
  2.  * Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18.  
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <aux.h>
  22.  
  23. #define auxMain
  24. #include <auxPrivate.h>
  25. #undef auxMain
  26.  
  27.  
  28. enum { NOT_SET, RGB_MODE, INDEX_MODE };
  29.  
  30. /**************************************************************************
  31.  *   Window parameters used for window initialization
  32.  **************************************************************************/
  33.  
  34. struct _win {
  35. GLint           x;
  36. GLint           y;
  37. GLsizei         width;
  38. GLsizei         height;
  39. GLsizei         aspectWidth;
  40. GLsizei         aspectHeight;
  41. }    win;
  42.  
  43. /**************************************************************************
  44.  *   Global variables and flags
  45.  **************************************************************************/
  46.  
  47. GLint           colormapMode = NOT_SET;
  48. GLboolean       initPosition = GL_FALSE;
  49. GLboolean    keepAspect   = GL_FALSE;
  50.  
  51. int             attribList[MAX_ATTRIB] = { None };
  52.  
  53.  
  54. /**************************************************************************
  55.  *   auxInit() - Open connection to server and initialize state variables
  56.  **************************************************************************/
  57.  
  58. GLvoid
  59. auxInit( GLvoid )
  60. {
  61.     GLint        i;
  62.     char         *displayName = (char *) NULL;
  63.     int erb, evb;
  64.  
  65.     if ( (displayName = getenv( "DISPLAY" )) == (char *) NULL )
  66.         displayName = ":0";
  67.  
  68.     auxState.display = XOpenDisplay( displayName );
  69.  
  70.     if ( auxState.display == (Display *) NULL ) {
  71.         char    str[80];
  72.  
  73.         sprintf( str, "Unable to open display '%s'", displayName );
  74.         auxFatalError( str );
  75.     }
  76.     if (!glXQueryExtension(auxState.display, &erb, &evb)) {
  77.         auxFatalError("No glx extension!");
  78.     }
  79.  
  80.  
  81. #ifdef DEBUG
  82.     XSetErrorHandler( auxXErrorHandler );
  83. #endif
  84.  
  85.     if ( atexit( auxCleanup ) )
  86.         auxFatalError( "Couldn't register auxCleanup " );
  87.  
  88.     auxState.screen = DefaultScreenOfDisplay( auxState.display );
  89.     auxState.screenNum = DefaultScreen( auxState.display );
  90.     auxState.rootWindow = RootWindow( auxState.display, auxState.screenNum );
  91.  
  92.     auxState.current = (auxWindow *) NULL;
  93.     auxState.head = (auxWindow *) NULL;
  94.     auxState.tail = (auxWindow *) NULL;
  95.  
  96.     auxState.rgbColormap = (Colormap) NULL;
  97.     auxState.indexColormap = (Colormap) NULL;
  98.  
  99.     auxInitCalled = GL_TRUE;
  100. }
  101.  
  102.  
  103. /**************************************************************************
  104.  *   auxKeepAspect() - Initialize window attribute variables
  105.  **************************************************************************/
  106.  
  107. void
  108. auxKeepAspect( GLsizei width, GLsizei height )
  109. {
  110.     if ( width <= ZERO )
  111.         auxFatalError( "Window width <= 0" );
  112.  
  113.     if ( height <= ZERO )
  114.         auxFatalError( "Window height <= 0" );
  115.  
  116.     win.aspectWidth = width;
  117.     win.aspectHeight = height;
  118.  
  119.     keepAspect = GL_TRUE;
  120. }
  121.  
  122. /**************************************************************************
  123.  *   auxInitPosition() - Initialize window attribute variables
  124.  **************************************************************************/
  125.  
  126. void
  127. auxInitPosition( GLint x, GLint y, GLsizei width, GLsizei height )
  128. {
  129.     win.x = x;
  130.     win.y = y;
  131.  
  132.     if ( width <= ZERO )
  133.         auxFatalError( "Window width <= 0" );
  134.  
  135.     if ( height <= ZERO )
  136.         auxFatalError( "Window height <= 0" );
  137.  
  138.     win.width = width;
  139.     win.height = height;
  140.  
  141.     initPosition = GL_TRUE;
  142. }
  143.  
  144.  
  145. /**************************************************************************
  146.  *   auxInitDisplayMode() - Initialize window modes
  147.  **************************************************************************/
  148.  
  149. GLvoid
  150. auxInitDisplayMode( GLuint mode )
  151. {
  152.     GLint      pos = ZERO;
  153.  
  154.     if ( ((mode & AUX_SINGLE) && (mode & AUX_DOUBLE)) ||
  155.      ((mode & AUX_INDEX) && (mode & AUX_RGBA)) )
  156.         auxFatalError( "Contradicting window modes" );
  157.  
  158.     if ( mode & AUX_DOUBLE )
  159.         attribList[pos++] = GLX_DOUBLEBUFFER;
  160.  
  161.     if ( mode & AUX_RGBA ) {
  162.         attribList[pos++] = GLX_RGBA;
  163.         /* setting R,G,and B _SIZEs to 1 should choose largest avail. */
  164.         attribList[pos++] = GLX_RED_SIZE;
  165.         attribList[pos++] = 1;
  166.         attribList[pos++] = GLX_GREEN_SIZE;
  167.         attribList[pos++] = 1;
  168.         attribList[pos++] = GLX_BLUE_SIZE;
  169.         attribList[pos++] = 1;
  170.  
  171.         if ( mode & AUX_ALPHA ) {
  172.             attribList[pos++] = GLX_ALPHA_SIZE;
  173.             attribList[pos++] = 1;
  174.         }
  175.  
  176.         colormapMode = RGB_MODE;
  177.     } else
  178.         colormapMode = INDEX_MODE;
  179.  
  180.  
  181.     if ( mode & AUX_DEPTH ) {
  182.         attribList[pos++] = GLX_DEPTH_SIZE;
  183.         attribList[pos++] = 1;
  184.     }
  185.  
  186.     if ( mode & AUX_STENCIL ) {
  187.         attribList[pos++] = GLX_STENCIL_SIZE;
  188.         attribList[pos++] = 1;
  189.     }
  190.  
  191.     if ( mode & AUX_ACCUM ) {
  192.         attribList[pos++] = GLX_ACCUM_RED_SIZE;
  193.         attribList[pos++] = 1;
  194.         attribList[pos++] = GLX_ACCUM_GREEN_SIZE;
  195.         attribList[pos++] = 1;
  196.         attribList[pos++] = GLX_ACCUM_BLUE_SIZE;
  197.         attribList[pos++] = 1;
  198.  
  199.         if ( mode & AUX_ALPHA ) {
  200.             attribList[pos++] = GLX_ACCUM_ALPHA_SIZE;
  201.             attribList[pos++] = 1;
  202.         }
  203.     }
  204.  
  205.     if ( mode & AUX_AUX ) 
  206.         auxNote( "AUX_AUX option not supported" );
  207.  
  208.     attribList[pos] = None;
  209.  
  210. #ifdef DEBUG
  211.     auxPrintAttributeList( attribList );
  212. #endif
  213. }
  214.  
  215.  
  216. /**************************************************************************
  217.  *   WaitForMapNotify() - delay function to make sure window is on screen
  218.  **************************************************************************/
  219.  
  220. static Bool
  221. WaitForMapNotify( Display *display, XEvent *event, XPointer data )
  222. {
  223.     Window       glxWindow = (Window) data;
  224.  
  225.     if ( event->type == MapNotify && event->xmap.window == glxWindow )
  226.         return GL_TRUE;
  227.     else
  228.         return GL_FALSE;
  229. }
  230.  
  231.  
  232. /**************************************************************************
  233.  *   auxInitWindow - initialize window
  234.  **************************************************************************/
  235.  
  236. void
  237. auxInitWindow( char *title )
  238. {
  239.     unsigned int                borderWidth = ZERO;
  240.     unsigned long               configMasks;
  241.     auxWindow                   *new;
  242.     XEvent                      event;
  243.     XSizeHints                  sizeHints;
  244.     XSetWindowAttributes        windowAttrib;
  245.     XTextProperty               windowProp;
  246.  
  247.     static GLint                windowId = ZERO;
  248.     int                i;
  249.     GLboolean             tryNewVisual = GL_FALSE;
  250.  
  251.     if ( !auxInitCalled )
  252.         auxInit();
  253.  
  254.     new = (auxWindow *) malloc( sizeof( auxWindow ) );
  255.  
  256.     new->id = windowId;
  257.     if ( initPosition ) {
  258.         new->x = win.x;
  259.         new->y = win.y;
  260.         new->width = win.width;
  261.         new->height = win.height;
  262.     } else {
  263.         new->x = 0;
  264.         new->y = 0;
  265.         new->width = 100;
  266.         new->height = 100;
  267.     }
  268.     new->prev = (auxWindow *) NULL;
  269.     new->next = (auxWindow *) NULL;
  270.  
  271.     if ( !auxState.head ) {
  272.         auxState.head = new;
  273.         auxState.tail = new;
  274.     } else {
  275.         auxState.tail->next = new;
  276.         auxState.tail = new;
  277.     }
  278.  
  279.     auxState.current = new;
  280.     windowId++;
  281.  
  282.     new->glxVisual = glXChooseVisual( auxState.display, auxState.screenNum,
  283.         attribList );
  284.  
  285.     if ( new->glxVisual == (XVisualInfo *) NULL )
  286.     {
  287.         for ( i = 0; (i < MAX_ATTRIB) && (attribList[i] != None); i++ )
  288.         {
  289.             switch( attribList[i] ) {
  290.                 case GLX_ALPHA_SIZE :
  291.                     i++;
  292.                     fprintf( stderr, 
  293.             "Unable to find visual with GLX_ALPHA_SIZE = %d, trying 0\n", 
  294.                     attribList[i] );
  295.                     attribList[i] = 0;
  296.                     tryNewVisual = GL_TRUE;
  297.                     break;
  298.  
  299.                 case GLX_ACCUM_ALPHA_SIZE :
  300.                     i++;
  301.                     fprintf( stderr, 
  302.             "Unable to find visual with GLX_ACCUM_ALPHA_SIZE = %d, trying 0\n", 
  303.                     attribList[i] );
  304.                     attribList[i] = 0;
  305.                     tryNewVisual = GL_TRUE;
  306.                     break;
  307.  
  308.                 default:
  309.                     break;
  310.             }
  311.             /* if attribList is changed, try a new visual, */
  312.             /* else check next attrib */
  313.             if ( tryNewVisual )
  314.             {
  315.                 new->glxVisual = glXChooseVisual( auxState.display, 
  316.                     auxState.screenNum, attribList );
  317.                 tryNewVisual = GL_FALSE;
  318.                 
  319.                 /* if a valid visual is found, break out */
  320.                 if ( new->glxVisual != (XVisualInfo *) NULL )
  321.                 {
  322.                     break;
  323.                 }
  324.             }
  325.         }
  326.         /* if still no valid visual, print fatal error and exit */
  327.         if ( new->glxVisual == (XVisualInfo *) NULL )
  328.         {
  329.             auxFatalError( "Unable to find visual" );
  330.         }
  331.     }
  332.     
  333. #ifdef DEBUG
  334.     auxPrintVisualInfo( new->glxVisual );
  335. #endif
  336.  
  337.     if ( colormapMode == RGB_MODE ) {
  338.         if ( !auxState.rgbColormap )
  339.             auxState.rgbColormap = XCreateColormap( auxState.display, 
  340.                 auxState.rootWindow, new->glxVisual->visual, AllocNone );
  341.  
  342.         new->colormap = auxState.rgbColormap;
  343.         windowAttrib.border_pixel = 0x0;
  344.     } else {
  345.         if ( !auxState.indexColormap )
  346.             auxState.indexColormap = XCreateColormap( auxState.display, 
  347.                 auxState.rootWindow, new->glxVisual->visual, AllocAll );
  348.  
  349.         new->colormap = auxState.indexColormap;
  350.         windowAttrib.border_pixel = BlackPixel( auxState.display,
  351.             auxState.screenNum );
  352.     }
  353.     colormapMode = NOT_SET;
  354.  
  355.     windowAttrib.colormap = new->colormap;
  356.     windowAttrib.event_mask = ExposureMask | StructureNotifyMask |
  357.         KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
  358.         ButtonMotionMask | PointerMotionMask;
  359.  
  360.     configMasks = CWBorderPixel | CWColormap | CWEventMask;
  361.  
  362.     new->glxWindow = XCreateWindow( auxState.display, auxState.rootWindow,
  363.         new->x, new->y, new->width, new->height, borderWidth, new->glxVisual->depth,
  364.         InputOutput, new->glxVisual->visual, configMasks, &windowAttrib );
  365.  
  366.     if ( initPosition ) {
  367.         sizeHints.flags = USPosition | PSize;
  368.         XSetWMNormalHints( auxState.display, new->glxWindow, &sizeHints );
  369.      }
  370.  
  371.      if ( keepAspect ) {
  372.         sizeHints.flags = PAspect;
  373.         sizeHints.min_aspect.x = sizeHints.max_aspect.x = win.aspectWidth;    
  374.         sizeHints.min_aspect.y = sizeHints.max_aspect.y = win.aspectHeight;    
  375.         XSetStandardProperties( auxState.display, new->glxWindow, title, title, None, NULL, 0, &sizeHints );
  376.      }
  377.  
  378.     XSetWMColormapWindows( auxState.display, new->glxWindow, &new->glxWindow,
  379.         ONE );
  380.     XStringListToTextProperty( &title, ONE, &windowProp );
  381.     XSetWMName( auxState.display, new->glxWindow, &windowProp );
  382.     XSetWMIconName( auxState.display, new->glxWindow, &windowProp );
  383.  
  384.     XMapWindow( auxState.display, new->glxWindow );
  385.     XIfEvent( auxState.display, &event, WaitForMapNotify,
  386.         (XPointer) new->glxWindow );
  387.  
  388.     new->glxContext = glXCreateContext( auxState.display, new->glxVisual, NULL,
  389.         GL_TRUE );
  390.  
  391.     auxWinSet( MINUS_ONE );
  392.  
  393.     initPosition = GL_FALSE;
  394.     keepAspect = GL_FALSE;
  395. }
  396.